package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.TitleDao;
import com.woniuxy.entity.Question;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.TitleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 罗蔚
 * @Date 2022/4/26 17:31
 * @Description TODO
 */
@Service
public class TitleServiceImpl implements TitleService {
    @Resource
    TitleDao titleDao;

    @Override
    public ResponseEntity showAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        //查询全部题目
        List<Question> list = titleDao.findChoiceQuestion();

        PageInfo<Question> pageInfo = PageInfo.of(list);

        return new ResponseEntity(200, "查询成功", pageInfo);
    }

    @Override
    public ResponseEntity findLike(Question question, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        if (question.getFid() == 0 ) {
            //没有fid，说明直接查询，否则是点击了左边的分类再查询
            List<Question> list = titleDao.findLike(question);
            PageInfo<Question> pageInfo = PageInfo.of(list);
            return new ResponseEntity(200, "查询成功", pageInfo);
        }else {
            List<Question> list = titleDao.findLikeAndFid(question);
            PageInfo<Question> pageInfo = PageInfo.of(list);
            return new ResponseEntity(200, "查询成功", pageInfo);
        }
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        //软删除，将is_delete 字段改为0
        int i = titleDao.updateOne(id);
        if (i != 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }

    @Override
    public ResponseEntity delete(Integer[] arr) {
        int i = titleDao.update(arr);
        if (i != 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }

}
